import java.util.Random;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Encryption {

    // 加密函数
    public static String encrypt(String plaintext) {
        int offset = 3;
        int length = plaintext.length();
        char[] chars = plaintext.toCharArray();
        // 加密过程（循环）
        for (int i = 0; i < length; i++) {
            chars[i] = (char) (chars[i] + i + offset);
        }

        // 改变字符串的顺序
        char firstChar = chars[0];
        char lastChar = chars[length - 1];
        chars[0] = lastChar;
        chars[length - 1] = firstChar;
        StringBuilder encrypted = new StringBuilder(new String(chars));
        encrypted.reverse();

        //返回密文
        return encrypted.toString();
    }

    // 解密函数
    public static String decrypt(String ciphertext) {
        char[] chars = new StringBuilder(ciphertext).reverse().toString().toCharArray();
        char firstChar = chars[0];
        char lastChar = chars[chars.length - 1];
        chars[0] = lastChar;
        chars[chars.length - 1] = firstChar;

        int offset = 3;
        int length = chars.length;

        // 解密过程（循环）
        for (int i = 0; i < length; i++) {
            chars[i] = (char) (chars[i] - i - offset);
        }

        //返回明文
        return new String(chars);
    }

        public String Judgment(String plaintext) {
            Scanner scanner = new Scanner(System.in);
            System.out.print("请输入要判断密码强度的密码：");
            String password = scanner.nextLine();
            scanner.close();

            if (password.length() < 8 || password.matches("[0-9]+") || password.matches("[a-zA-Z]+")) {
                System.out.println("密码强度为弱");
            } else {
                Pattern pattern = Pattern.compile("[A-Za-z\\d@$!%*?&]+");
                Matcher matcher = pattern.matcher(password);
                if (matcher.matches() && password.matches(".*\\d.*") && password.matches(".*[a-z].*") && password.matches(".*[A-Z].*")) {
                    System.out.println("密码强度为高");
                } else {
                    System.out.println("密码强度为中");
                }
            }
            return password;
        }

        private static final String LOWERCASE_LETTERS = "abcdefghijklmnopqrstuvwxyz";
        private static final String UPPERCASE_LETTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        private static final String NUMBERS = "0123456789";

        public String Rpassword(String str){
            Scanner sc = new Scanner(System.in);
            System.out.println("请设定密码的长度：");
            int passwordLength = sc.nextInt();// 用户设定的密码长度
            String password = generateStrongPassword(passwordLength);
            System.out.println("Your strong password is: " + password);
            return password;
        }

        private String generateStrongPassword(int length) {
            Random random = new Random();
            StringBuilder password = new StringBuilder();
            boolean hasLowercase = false;
            boolean hasUppercase = false;
            boolean hasNumber = false;

            // 生成包含至少一个数字、一个小写字母和一个大写字母的随机密码
            while (password.length() < length || !hasLowercase || !hasUppercase || !hasNumber) {
                int type = random.nextInt(3); // 随机选择数字、小写字母或大写字母
                if (type == 0) { // 生成数字
                    password.append(NUMBERS.charAt(random.nextInt(NUMBERS.length())));
                    hasNumber = true;
                } else if (type == 1) { // 生成小写字母
                    password.append(LOWERCASE_LETTERS.charAt(random.nextInt(LOWERCASE_LETTERS.length())));
                    hasLowercase = true;
                } else { // 生成大写字母
                    password.append(UPPERCASE_LETTERS.charAt(random.nextInt(UPPERCASE_LETTERS.length())));
                    hasUppercase = true;
                }
            }

            return password.toString();
        }
    }

